package com.sw.c.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * @author xiaolwi
 * @time 2022/12/28 15:30
 */

public class ResponseUtil {

    public static ResponseMO response(int code, String msg, Object data, String debugInfo) {
        ResponseMO responseMO = new ResponseMO();
        responseMO.setCode(code);
        responseMO.setMsg(msg);
        responseMO.setData(data);
        responseMO.setDebugInfo(debugInfo);
        return responseMO;
    }

    public static ResponseMO response(int code, String msg, Object data) {
        return response(code, msg, data, null);
    }

    public static ResponseMO response(int code, String msg) {
        return response(code, msg, null, null);
    }

    public static ResponseMO success(String msg) {
        return response(ResponseMO.RESPONSE_CODE_SUCCESS, msg, null, null);
    }

    public static ResponseMO success() {
        return response(ResponseMO.RESPONSE_CODE_SUCCESS, null, null, null);
    }

    public static ResponseMO successWithData(String msg, Object data) {
        return response(ResponseMO.RESPONSE_CODE_SUCCESS, msg, data, null);
    }

    public static ResponseMO successWithDataList(String msg, Object data) {
        Map<String, Object> resuilt = new HashMap<String, Object>();
        resuilt.put("list", data);
        return response(ResponseMO.RESPONSE_CODE_SUCCESS, msg, resuilt, null);
    }

    public static ResponseMO successWithDataList2(String msg, Object data, int total) {
        Map<String, Object> resuilt = new HashMap<String, Object>();
        resuilt.put("list", data);
        resuilt.put("total", total);
        return response(ResponseMO.RESPONSE_CODE_SUCCESS, msg, resuilt, null);
    }

    public static ResponseMO successWithDataList3(String msg, Object data, int total) {
        Map<String, Object> resuilt = new HashMap<String, Object>();
        if (data == null) {
            resuilt.put("list", new ArrayList<>());
            resuilt.put("total", 0);
        } else {
            resuilt.put("list", data);
            resuilt.put("total", total);
        }
        return response(ResponseMO.RESPONSE_CODE_SUCCESS, msg, resuilt, null);
    }

    public static ResponseMO error(int code, String message) {
        return response(code, message);
    }

    public static ResponseMO error(String message, String debugInfo) {
        return response(ResponseMO.RESPONSE_CODE_FAILURE, message, null, debugInfo);
    }

    public static ResponseMO error(String message) {
        return error(ResponseMO.RESPONSE_CODE_FAILURE, message);
    }

    public static ResponseMO errorWithData(String message, Object data) {
        return response(ResponseMO.RESPONSE_CODE_FAILURE, message, data, null);
    }
}
